//
//  QXMaskView.m
//  WanCaiC8
//
//  Created by tangang on 2018/3/7.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "QXMaskView.h"
#import "TimeModel.h"
@implementation QXMaskView

- (instancetype)init{
    if (self=[super init]) {
        self.backgroundColor=[UIColor colorWithWhite:0 alpha:0.0];
        [self setFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeSelf)];
        [self addGestureRecognizer:tap];
        
        UIWindow *window=[UIApplication sharedApplication].keyWindow;
        [window addSubview:self];
    }
    return self;
}

- (instancetype)initWithRightItemTitles:(NSArray *)rightItems{
    self=[self init];
    if (self) {
        [self moreBtnClick:rightItems];
    }
    return self;
}

#define BGVTAG 101
///点击右侧
- (void)moreBtnClick:(NSArray *)rightItems{
    WS(weakSelf);
    UIImageView *accView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"red_angle1"]];
    [self addSubview:accView];
    [accView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf).with.offset(64);
        make.right.equalTo(weakSelf).with.offset(-30);
        make.size.mas_equalTo(accView.image.size);
    }];
    
    UIView *bgV=[[UIView alloc]init];
    bgV.layer.cornerRadius=4.0;
    bgV.tag=BGVTAG;
    bgV.backgroundColor=NAVCOLOR;
    [self addSubview:bgV];
    [bgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(accView.mas_bottom).with.offset(-1);
        make.right.equalTo(weakSelf).with.offset(-10);
        make.size.mas_equalTo(CGSizeMake(100, rightItems.count*40));
    }];
    
    for (int i=0;i<rightItems.count;i++) {
        UILabel *iv = [[UILabel alloc] init];
        iv.font = [UIFont systemFontOfSize:14];
        if ([rightItems[i] isKindOfClass:[TimeModel class]]) {
            TimeModel *model=rightItems[i];
            iv.text = model.Value;
        }else{
            iv.text = rightItems[i];
        }
        
        iv.textColor=[UIColor whiteColor];
        iv.textAlignment = NSTextAlignmentCenter;
        iv.userInteractionEnabled = YES;
        iv.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(accessoryMoreViewClick:)];
        [iv addGestureRecognizer:tap];
        [bgV addSubview:iv];
        [iv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(bgV);
            make.top.equalTo(bgV).with.offset(40*i);
            make.height.mas_equalTo(@40);
        }];
        if (i<rightItems.count-1) {
            UIView *lineView=[[UIView alloc]init];
            lineView.backgroundColor=[UIColor whiteColor];
            [bgV addSubview:lineView];
            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(bgV).with.offset(5);
                make.right.equalTo(bgV).with.offset(-5);
                make.top.equalTo(iv.mas_bottom);
                make.height.mas_equalTo(@.7);
            }];
        }
    }
}

#pragma mark-按钮点击事件
- (void)accessoryMoreViewClick:(UITapGestureRecognizer *)tap{
    [self removeSelf];
    if (self.JXBMaskViewBlock) {
        _JXBMaskViewBlock(tap.view.tag);
    }
}

- (void)removeSelf{
    [self removeFromSuperview];
}

@end
